package com.tj.view.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import com.tj.view.R;

import static android.view.View.MeasureSpec.getMode;
import static android.view.View.MeasureSpec.getSize;

/**
 * Created by Administrator on 2017/1/22 0022.
 * 自定义
 */
public class CustomImageView extends View {

    private static final String TAG = "CustomImageView";
    private static final int TYPE_CIRCLE = 0;
    private static final int TYPE_ROUND = 1;
    private Bitmap mSrc;
    private int type;
    private int mRadius;
    /**
     * 控件的宽度
     */
    private int mWidth;

    /**
     * 控件的高度
     */
    private int mHeight;

    public CustomImageView(Context context) {
        this(context, null);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.CustomImageView, defStyleAttr, 0);

        int n = typedArray.getIndexCount();
        for (int i = 0; i < n; i++) {
            int index = typedArray.getIndex(i);
            switch (index) {
                case R.styleable.CustomImageView_src:

                    int resourceId = typedArray.getResourceId(index, 0);
                    mSrc = BitmapFactory.decodeResource(getResources(), resourceId);
                    break;
                case R.styleable.CustomImageView_type:
                    type = typedArray.getInt(R.styleable.CustomImageView_type, 0);
                    break;
                case R.styleable.CustomImageView_borderRadius:
                    //TypedValue.COMPLEX_UNIT_DIP是dp转换px
                    mRadius = typedArray.getDimensionPixelSize(R.styleable.CustomImageView_borderRadius,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP
                                    , 10f, getResources().getDisplayMetrics()));
                    Log.d(TAG, "init: mRadius=" + mRadius);
                    break;
                default:
                    break;
            }
        }
        //不要忘记了!!!
        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int mWidthMode = getMode(widthMeasureSpec);
        int mWidthSize = getSize(widthMeasureSpec);
        if (mWidthMode == MeasureSpec.EXACTLY) {
            mWidth = mWidthSize;
        } else {
            //
            int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();
            if (mWidthMode == MeasureSpec.AT_MOST) {//wrap_content
                //为何是取小的?
                mWidth = Math.min(mWidthSize, desireByImg);
            } else {
                mWidth = desireByImg;
            }
        }

        int mHeightMode = MeasureSpec.getMode(heightMeasureSpec);
        int mHeightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (mHeightMode == MeasureSpec.EXACTLY) {//wrap_content
            mHeight = mHeightSize;
        } else {
            int desireHeight = mSrc.getHeight() + getPaddingTop() + getPaddingBottom();
            if (mHeightMode == MeasureSpec.AT_MOST) {
                mHeight = Math.min(desireHeight, mHeightSize);
            } else {
                mHeight = desireHeight;
            }
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //根据自定义的type类型
        switch (type) {
            case TYPE_CIRCLE:
                int min = Math.min(mWidth, mHeight);
                /**
                 * 长度如果不一致，按小的值进行压缩
                 */
                mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);

                canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);

                break;
            case TYPE_ROUND:
                canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);
                break;
            default:
                break;
        }
    }

    /**
     * 缩放的图片
     *
     * @param source
     * @param min
     * @return
     */
    public Bitmap createCircleImage(Bitmap source, int min) {

        final Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(
                min, min, Bitmap.Config.ARGB_8888);
        //???
        /**
         * 产生一个同样大小的画布
         */
        Canvas canvas = new Canvas(target);

        /**
         * 首先绘制圆形
         */
        canvas.drawCircle(min / 2, min / 2, min / 2, mPaint);

        /**
         * 使用SRC_IN，参考上面的说明
         */
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        /**
         * 绘制图片
         */
        canvas.drawBitmap(source, 0, 0, mPaint);

        return target;//返回target
    }

    /**
     * 根据原图添加圆角
     *
     * @param source
     * @return
     */
    private Bitmap createRoundConerImage(Bitmap source) {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);

        //先绘制矩形
        RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rect, mRadius, mRadius, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }
}
